package hiatao.puzzle.adapter;

import haitao.puzzle.R;
import hiatao.puzzle.activity.PuzzleGameActivity;
import hiatao.puzzle.config.MyApplication;
import hiatao.puzzle.config.URLS;
import hiatao.puzzle.entity.PuzzleInfo;
import hiatao.puzzle.entity.User;
import hiatao.puzzle.utils.ImageUtils;
import hiatao.puzzle.utils.PixelUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class PuzzleListAdapter extends BaseAdapter {

	private ArrayList<PuzzleInfo> mInfos;
	private LayoutInflater inflater;
	private Context context;
	private User u;

	// public static final String STATUS_UNFINISH_NAME="(已派发)";
	// public static final String STATUS_FINISH_NAME="(不存在)";
	//
	// public static final String STATUS_SENDED_CODE="1";
	// public static final String STATUS_UNEXIST_CODE="-1";

	public PuzzleListAdapter( User u,Context context) {
		this.inflater = LayoutInflater.from(context);
		this.context = context;
		this.u=u;
		mInfos = new ArrayList<PuzzleInfo>();

	}

    public void clear() {
    	mInfos.clear();
    }
    
    @Override
    public int getCount() {
    	return mInfos.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mInfos.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    public void addItemLast(List<PuzzleInfo> datas) {
        mInfos.addAll(datas);
        notifyDataSetChanged();
    }

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.news_list_item_view,parent,false);
			 holder.imageView = (ImageView) convertView.findViewById(R.id.news_pic);
			 holder.imageView2 = (ImageView) convertView.findViewById(R.id.news_pic2);
			 holder.mounth_tv = (TextView) convertView.findViewById(R.id.mounth_tv);
			 holder.day_tv = (TextView) convertView.findViewById(R.id.day_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		LayoutParams lp = (LayoutParams) holder.imageView.getLayoutParams();
		int s= PixelUtil.getScreenWidth(context)-PixelUtil.dp2px(context, 16)-PixelUtil.dp2px(context, 70);
		lp.width = s/2;
		lp.height=s/2;
		LayoutParams lp2 = (LayoutParams) holder.imageView.getLayoutParams();
		lp2.width = s/2;
		lp2.height=s/2;
		holder.imageView.setLayoutParams(lp);
		holder.imageView2.setLayoutParams(lp2);
		
		ImageUtils.loadImg(mInfos.get(position).getPicture(), holder.imageView);//0 1 1,3 zn+1
		ImageUtils.loadImg(mInfos.get(position).getPicture2(), holder.imageView2);//0 2 2N+2
		holder.imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, PuzzleGameActivity.class);
				intent.putExtra("URL", mInfos.get(position).getPicture());
				intent.putExtra("HEADURL",u.getHeadUrl());
				intent.putExtra("NAME",u.getName());
				intent.putExtra("INTRO",u.getIntro());
				
				context.startActivity(intent);
			}
		});
		holder.imageView2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, PuzzleGameActivity.class);
				intent.putExtra("URL", mInfos.get(position).getPicture2());
				intent.putExtra("HEADURL",u.getHeadUrl());
				intent.putExtra("NAME",u.getName());
				intent.putExtra("INTRO",u.getIntro());
				context.startActivity(intent);
			}
		});
		
		if(position==0){
			holder.mounth_tv.setText("12月");
			holder.day_tv.setText("7");
		}else if (position==1){
			holder.mounth_tv.setText("9月");
			holder.day_tv.setText("12");
		}else if (position==2){
			holder.mounth_tv.setText("6月");
			holder.day_tv.setText("1");
		}else if (position==3){
			holder.mounth_tv.setText("5月");
			holder.day_tv.setText("7");
		}else{
			holder.mounth_tv.setText("4月");
			holder.day_tv.setText("1");
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView imageView;
		ImageView imageView2;
		public TextView tv_content;
		public TextView tv_title;
		public TextView mounth_tv;
		public TextView day_tv;
		public TextView tv_time;
		public ImageView img_dot;
	}

}
